<?php


namespace app\common\controller;

use Jenssegers\Agent\Agent;
use think\Request;

/**
 * Class LoginLog
 * @package app\common\controller
 */
class LoginLog
{
    /**
     * 登录成功日志
     * @param array $param
     * @param string $msg
     * @param $login_time
     */
    public static function LoginSuccessLog(array $param, string $msg, $login_time)
    {
        $agent = new Agent();
        $agent->setUserAgent(Request::instance()->header('user-agent'));
        $logInfo['username'] = $param['username'];
        $logInfo['login_ip'] = $param['login_ip'];
        $logInfo['login_location'] = Util::get_location_by_ip($param['login_ip']);
        $logInfo['browser'] = $agent->browser();
        $logInfo['os'] = $agent->platform();
        $logInfo['status'] = 0;
        $logInfo['msg'] = $msg;
        $logInfo['login_time'] = $login_time;
        $loginLog = new \app\api\model\LoginLog();
        $loginLog->save($logInfo);
    }

    /**
     * 登录失败日志
     * @param array $param
     * @param string $msg
     * @param $login_time
     */
    public static function LoginErrorLog(array $param, string $msg, $login_time)
    {
        $agent = new Agent();
        $agent->setUserAgent(Request::instance()->header('user-agent'));
        $logInfo['username'] = $param['username'];
        $logInfo['login_ip'] = $param['login_ip'];
        $logInfo['login_location'] =  Util::get_location_by_ip($param['login_ip']);
        $logInfo['browser'] = $agent->browser();
        $logInfo['os'] = $agent->platform();
        $logInfo['status'] = 1;
        $logInfo['msg'] = $msg;
        $logInfo['login_time'] = $login_time;
        $loginLog = new \app\api\model\LoginLog();
        $loginLog->save($logInfo);
    }
}